package com.personal.system.service.impl.sys;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.personal.model.system.SysLoginLog;
import com.personal.model.vo.SysLoginLogQueryVo;
import com.personal.system.mapper.sys.LoginLogMapper;
import com.personal.system.service.LoginLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    @Override
    public void recordLoginLog(String username, Integer status,
                               String ipaddr, String message) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setStatus(status);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setMsg(message);
        loginLogMapper.insert(sysLoginLog);
    }

    //条件分页查询登录日志
    @Override
    public IPage<SysLoginLog> selectPage(long page, long limit, SysLoginLogQueryVo sysLoginLogQueryVo) {
        //创建page对象
        Page<SysLoginLog> pageParam = new Page(page, limit);
        //获取条件值
        String username = sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();
        //封装条件
        QueryWrapper<SysLoginLog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wrapper.like("username", username);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time", createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.le("DATE_FORMAT(create_time,'%Y-%m-%d')", createTimeEnd);
        }
        wrapper.orderByDesc("create_time");

        IPage<SysLoginLog> pageModel = loginLogMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    @Override
    public Map<String, Object> loginCount(){

        Map<String, Object> allMap = new HashMap<>();
        List<Map<String,String>> logs = loginLogMapper.selectCouList(Wrappers.emptyWrapper());
        CollUtil.sort(logs, Comparator.comparing(o -> o.get("createTime")));
        List<String> dataTime = logs.stream().map(e -> e.get("createTime")).collect(Collectors.toList());
        List<String> cou = logs.stream().map(e -> Convert.toStr(e.get("cou"))).collect(Collectors.toList());
        Map<String, Object> zxMap = new HashMap<>();
        zxMap.put("xValue", dataTime);
        zxMap.put("yValue", cou);
        allMap.put("zxMap", zxMap);

        List<Object> yuMap = new ArrayList<>();
        Map<String, Object> one = new HashMap<>();
        one.put("name", "vue");one.put("value", 50);
        yuMap.add(one);
        Map<String, Object> two = new HashMap<>();
        two.put("name", "java");two.put("value", 75);
        yuMap.add(two);
        Map<String, Object> thr = new HashMap<>();
        thr.put("name", "c++");thr.put("value", 40);
        yuMap.add(thr);
        Map<String, Object> four = new HashMap<>();
        four.put("name", ".net");four.put("value", 20);
        yuMap.add(four);
        allMap.put("yuMap", yuMap);

        return allMap;
    }

}
